<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules" v-bind="layout">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-model-item label="工种编码" prop="workTypeCode">
              <a-input v-model="model.workTypeCode" placeholder="请输入工种编码" :disabled="!!model.id"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工种名称" prop="workTypeName">
              <a-input v-model="model.workTypeName" placeholder="请输入工种名称"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-model-item label="排序" prop="sortOrder">
              <a-input-number v-model="model.sortOrder" placeholder="请输入排序" :min="0" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" prop="status">
              <j-dict-select-tag v-model="model.status" dictCode="valid_status" placeholder="请选择状态"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="工种描述" prop="description">
              <a-textarea v-model="model.description" placeholder="请输入工种描述" :rows="3"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="备注" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注" :rows="2"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'WorkTypeModal',
    components: {
    },
    data () {
      return {
        layout: {
          labelCol: {
            xs: { span: 24 },
            sm: { span: 5 },
          },
          wrapperCol: {
            xs: { span: 24 },
            sm: { span: 16 },
          },
        },
        title:"",
        visible: false,
        model: {},
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          workTypeCode: [
            { required: true, message: '请输入工种编码!' },
            { validator: this.validateWorkTypeCode }
          ],
          workTypeName: [
            { required: true, message: '请输入工种名称!' }
          ],
          status: [
            { required: true, message: '请选择状态!' }
          ]
        },
        url: {
          add: "/mes/worktype/add",
          edit: "/mes/worktype/edit",
          checkWorkTypeCode: "/mes/worktype/checkWorkTypeCode"
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(this.model)
        })
        if(!this.model.id){
          this.title = "新增工种";
        }else{
          this.title = "编辑工种";
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      handleCancel () {
        this.close();
      },
      // 验证工种编码唯一性
      validateWorkTypeCode(rule, value, callback) {
        if (!value) {
          callback();
          return;
        }
        
        let params = {
          workTypeCode: value,
          excludeId: this.model.id || ''
        };
        
        getAction(this.url.checkWorkTypeCode, params).then((res) => {
          if (res.success) {
            if (res.result) {
              callback(new Error('工种编码已存在，请使用其他编码'));
            } else {
              callback();
            }
          } else {
            callback();
          }
        }).catch(() => {
          callback();
        });
      }
    }
  }
</script>
